package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserThief;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserThiefDaoImpl extends TempDataObjectDao<UserThief> {

    public static String getUserThiefSql = "select * from t_u_thief where userId=? and getReward = 0";

    public Map<String, UserThief> getUserThiefMap(long userId) {
        Map<String, UserThief> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserThiefSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserThief userThief = parse(rs);
                map.put(userThief.getThiefUid(), userThief);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUserThiefSql = "update t_u_thief set type=?,crimePlaceId=?,workBeginTime=?,createTime=?,getReward=? where userId=? and thiefUid=?";

    @Override
    protected boolean updateData(UserThief userThief) {
        userThief.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userThief.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, userThief.getCrimePlaceId()));
        para.put(i++, new DbParameter(Types.BIGINT, userThief.getWorkBeginTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userThief.getCreateTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userThief.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, userThief.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userThief.getThiefUid()));
        if (getDb().executeNoneQuery(updateUserThiefSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserThiefSql = "insert into t_u_thief(userId,thiefUid,type,crimePlaceId,workBeginTime,createTime,getReward) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserThief userThief) {
        userThief.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userThief.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userThief.getThiefUid()));
        para.put(i++, new DbParameter(Types.INTEGER, userThief.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, userThief.getCrimePlaceId()));
        para.put(i++, new DbParameter(Types.BIGINT, userThief.getWorkBeginTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userThief.getCreateTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userThief.isGetReward()));
        if (getDb().executeNoneQuery(insertUserThiefSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserThief parseData(ResultSet rs) throws SQLException {
        UserThief userThief = new UserThief();
        userThief.setUserId(rs.getLong("userId"));
        userThief.setThiefUid(rs.getString("thiefUid"));
        userThief.setType(rs.getInt("type"));
        userThief.setCrimePlaceId(rs.getInt("crimePlaceId"));
        userThief.setWorkBeginTime(rs.getLong("workBeginTime"));
        userThief.setCreateTime(rs.getLong("createTime"));
        userThief.setGetReward(rs.getBoolean("getReward"));
        return userThief;
    }

}
